package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskPersonRelationDao;
import cn.topevery.garden.entity.po.TaskPersonRelation;
import cn.topevery.garden.entity.query.TaskPersonRelationQuery;
import cn.topevery.garden.entity.vo.TaskPersonRelationVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 人员任务关系表
 *
 * @author code-generate
 * @date 2021-09-02
 */
@Repository
public class TaskPersonRelationDaoImpl extends BaseDaoImpl<TaskPersonRelation> implements TaskPersonRelationDao {
    @Override
    public <P extends DbCsp> List<TaskPersonRelation> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<TaskPersonRelationVO> listPage(TaskPersonRelationQuery query) {
        String table = "t_task_person_relation ttpr";

        String columns = "ttpr.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttpr.c_id desc");
        sqlQueryMate
                .where(true, "ttpr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getTaskId() != null, "ttpr.c_task_id", ScriptConditionEnum.Eq, query.getTaskId());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskPersonRelationVO.class);
    }

    @Override
    public void deleteByTaskId(Integer userId, Integer taskId) {
        String table = "t_task_person_relation ttpr";

        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate
                .setColumnValue(true, "ttpr.c_db_status", 1)
                .setColumnValue(true, "ttpr.c_db_last_update_id", userId)
                .where(true, "ttpr.c_task_id", ScriptConditionEnum.Eq, taskId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public void deleteByUserId(Integer userId) {
        String table = "t_task_person_relation ttpr";

        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate
                .setColumnValue(true, "ttpr.c_db_status", 1)
                .setColumnValue(true, "ttpr.c_db_last_update_id", userId)
                .where(true, "ttpr.c_person_id", ScriptConditionEnum.Eq, userId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public TaskPersonRelation getByPersonId(Integer personId) {
        String table = "t_task_person_relation ttpr";

        String columns = "ttpr.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "ttpr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "ttpr.c_person_id", ScriptConditionEnum.Eq, personId);
        AtomicReference<TaskPersonRelation> taskPersonRelation = new AtomicReference<>(new TaskPersonRelation());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            taskPersonRelation.set(dbCommand.executeToEntity(TaskPersonRelation.class));
        });
        return taskPersonRelation.get();
    }

    @Override
    public TaskPersonRelation getByTaskId(Integer taskId) {
        String table = "t_task_person_relation ttpr";

        String columns = "ttpr.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "ttpr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "ttpr.c_task_id", ScriptConditionEnum.Eq, taskId);
        AtomicReference<TaskPersonRelation> taskPersonRelation = new AtomicReference<>(new TaskPersonRelation());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            taskPersonRelation.set(dbCommand.executeToEntity(TaskPersonRelation.class));
        });
        return taskPersonRelation.get();
    }
}
